package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TClient;
import com.wit.witmg.entity.TEquip;
import com.wit.witmg.mapper.TClientMapper;
import com.wit.witmg.service.ITClientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-02-08
 */
@Service
public class TClientServiceImpl extends ServiceImpl<TClientMapper, TClient> implements ITClientService {

    @Override
    public Object add(HttpServletRequest request, String json) {
        TClient t= JSON.parseObject(json,TClient.class);
        if(t.getId()>0){
            boolean bl=  updateById(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_200);
            }
        }else{
            boolean bl= save(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_504);
            }
        }
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, String tp, Integer oid, String bn, Integer id) {
        //bn返回bn开头包含参数的数组，oid部门编号，返回oid以参数开头并包含参数的数据，为空时返回全部；
        // tp返回tp相同的数组；id返回单条数据，常用于连表查询。
        QueryWrapper<TClient> qw=new QueryWrapper<>();
        if(bn!=null){
            qw.like("bn",bn);
            return JsonResult.success(list(qw));
        }else if(id!=null){
            qw.eq("id",id);
            qw.last("limit 1");
            return JsonResult.success(getOne(qw));
        }else if(tp!=null){
            qw.eq("tp",tp);
            return JsonResult.success(list(qw));
        }else{
            if(oid!=null){
                qw.eq("oid",oid);
            }
            return JsonResult.success(list(qw));
        }
    }

    @Override
    public Object del(HttpServletRequest request, Integer id) {
        boolean bl= removeById(id);
        if(bl) {
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }
}
